/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ScannerParser;

import java.util.*;
import java.util.regex.*;
/**
 *
 * @author jefoutta
 */
public class ThreeDirectionInstruction {
    private LinkedList<String> instructions;
    private LinkedList<String> types;
    private HashMap<String,Integer> typeCodes;

    public ThreeDirectionInstruction(){
        instructions = new LinkedList();
        types = new LinkedList();
        typeCodes = new HashMap();
        typeCodes.put("methodStart", 0);
        typeCodes.put("methodSize", 1);
        typeCodes.put("methodEnd", 2);
        typeCodes.put("ifStart", 3);
        typeCodes.put("whileStart", 4);
        typeCodes.put("return", 5);
        typeCodes.put("assign", 6);
        typeCodes.put("aritopBinary", 7);
        typeCodes.put("aritopUnary", 8);
        typeCodes.put("relopCmp", 9);
        typeCodes.put("relopIf", 10);
        typeCodes.put("relopIfFalse", 11);
        typeCodes.put("functionStart", 12);
        typeCodes.put("functionParam", 13);
        typeCodes.put("functionCall", 14);
        typeCodes.put("cmpAssign0", 15);
        typeCodes.put("cmpAssign1", 16);
        typeCodes.put("gotoInconditional", 17);
    }

    public void addInstruction(String instr, String ttype){
        instructions.addLast(instr);
        types.addLast(ttype);
    }

    public void addInstruction(ThreeDirectionInstruction tdi){
        if(tdi != null){
            for(int i = 0; i < tdi.instructions.size();i++){
                instructions.addLast(tdi.getInstruction(i));
                types.addLast(tdi.getType(i));
            }
        }
    }

    public String getInstruction(int index){
        return instructions.get(index);
    }

    public String getType(int index){
        return types.get(index);
    }

    public int getTypeCode(String stype){
        return typeCodes.get(stype);
    }

    public int getSize(){
        return instructions.size();
    }

    public Integer getFirstType(String str,int startIndex){
        int i = startIndex;
        while(i < instructions.size()){
            if(types.get(i).equals(str))
                return i;
            i++;
        }
        return -1;
    }

    public Integer getLastType(String str, int startIndex){
        int i = startIndex;
        while(i < instructions.size()){
            if(types.get(i).equals(str))
                return i;
            i--;
        }
        return -1;
    }

    public Integer getFirstInstruction(String str, int startIndex){
        int i = startIndex;
        while(i < instructions.size()){
            if(instructions.get(i).equals(str))
                return i;
            i++;
        }
        return -1;
    }

    public void setInstruction(int index, String str){
        instructions.set(index, str);
    }
    
    public void assembly(){
        String stringTemporal;
        for(int i = 0; i < types.size(); i++){
            if(types.get(i).equals("functionStart")){
                stringTemporal = instructions.get(getFirstType("functionCall",i));
                stringTemporal = stringTemporal.split("call ")[1];
                stringTemporal = stringTemporal.split(" ")[0];
                System.out.println("se esta buscando el Proc "+stringTemporal+"---------------------------------------------------------------");
                stringTemporal = instructions.get(getFirstType("methodSize",getFirstInstruction("Proc "+stringTemporal, 0)));
                instructions.set(i, "Push "+stringTemporal);
            }
        }
    }

}
